'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 撤销公会申请
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var moment = require('moment');
var constantUtil = require('../../../../util/constantUtil.js');
var Code = require('../../../../consts/code');
var unionGroupModel = require('../../../../models/unionGroupModel');
var unionGroupConf = require('../../../../config/unionGroupConf');
var PlayerManager = require('../../../../libs/playerManager');

/**
 * 撤销公会申请
 */

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var union_id = msg.union_id;

    if (!union_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    async.waterfall(
        [
            function (cb) {
                unionGroupModel.getById(union_id, cb);
            },
            function (model, cb) {
                if (!model.deleteApply(role_id)) {
                    return next(null, {
                        code: Code.UNION_NOT_APPLY
                    });
                }
                model.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK
            });
        }
    );
};
